import java.util.ArrayList;
import java.util.Stack;

public class Solution {
    public boolean IsPopOrder(int [] pushA,int [] popA) {
        // 辅助站栈
        /*
        if (pushA.length == 0) {
            return true;
        }

        Stack<Integer> stack = new Stack<>();
        int index = 0;

        for (int i: pushA) {
            stack.push(i);
            while (!stack.isEmpty() && stack.peek() == popA[index]) {
                stack.pop();
                index++;
            }
        }
        
        return stack.isEmpty();
        */ 

        // 原地栈
        if (pushA.length == 0) {
            return true;
        }

        int peek = 0;
        int index = 0;

        for (int i: pushA) {
            pushA[peek] = i;
            while(peek >= 0 && pushA[peek] == popA[index]) {
                peek--;
                index++;
            }
            peek++;
        }

        return peek == 0;
    }
}